package com.edu.web.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.edu.entity.User;
import com.edu.service.UserService;
import com.edu.service.impl.UserServiceImpl;


/**
 * Servlet implementation class loginservet
 */
@WebServlet("/loginservet")
public class loginservet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public loginservet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int width=90;
		int height=50;
		//1：创建生成一个图片对象
		BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);	
		//2：美化图片
		Graphics g = img.getGraphics();//添加画笔
		//2.1 添加背景颜色		
		g.setColor(Color.pink);//设置画笔颜色 
		g.fillRect(0, 0, width, height);//填充		
		//3、画边框
		g.setColor(Color.blue);//设置画笔颜色 
		g.drawRect(0, 0, width-1, height-1);//画边框
		
		//4、初始化验证码字符
		String str ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456";
		Random ran= new Random();//随机
		String code1="";//session 服务端验证
		g.setColor(Color.red);//画笔颜色
		g.setFont(new Font("楷体",800,30));//字体
		for(int i=0;i<4;i++) {//随机4个
			int index = ran.nextInt(str.length());//随机
			char a = str.charAt(index);//获取对应下标的字符
			code1+=a;//拼接存session
			g.drawString(a+"", (i+1)*15, 35);//字符写入图片
		}
		//g.drawString(code1, 17, 35);//统一一次写入

		//5、画干扰线
		g.setColor(Color.green);//干扰线颜色
		for(int i=0;i<10;i++) {
			//干扰线位置
			g.drawLine(ran.nextInt(width), ran.nextInt(height),ran.nextInt(width), ran.nextInt(height));
		}
		System.out.println(code1);
		//6：将验证码 存入session
		request.getSession().setAttribute("code", code1);//存session
		ImageIO.write(img, "jpg", response.getOutputStream());//输出到页面
		
	}
	
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	/**
	 *
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String userName = request.getParameter("userName");
		String userPass = request.getParameter("userPass");
		
		User user = new User(userName,userPass);
		UserService userService = new UserServiceImpl();
		
	
		HttpSession session = request.getSession();
		//1：获取输入的验证码
		String code = request.getParameter("code");
		//2：获取session验证码
		String sessionCode = (String)session.getAttribute("code");
		boolean isLogin = false; 
		if(!userName.equals("")||userPass.equals("")) {
			isLogin = userService.resrch(userName, userPass);
			if(!code.equals(sessionCode)) {
				isLogin = false; 
				String mes = "验证码错误！"; 
				request.setAttribute("mes", mes);
				request.getRequestDispatcher("login.jsp").forward(request, response);
			}
		}
		if(isLogin) {
			if("www".equals(userName)||"qwer".equals(userName)) {
				request.getRequestDispatcher("background.jsp").forward(request, response);
			}else {
				request.getRequestDispatcher("index.jsp").forward(request, response);
			}
		
			
		}else {
			String meg = "用户名或密码错误！"; 
			request.setAttribute("meg", meg);
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
	
	
	}
}


		
	


